<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        img {
            vertical-align: top;
        }

        .valida-bar {
            width: 600px;
            margin: 0 auto;
            margin-top: 100px;
        }

        .valida-bar .valida-rand {
            position: relative;
        }

        .valida-bar .valida-rand img {
            width: 100%;
            height: 263px;
        }

        .valida-a,
        .valida-b,
        .ball {
            width: 45px;
            height: 45px;
            position: absolute;
        }

        .valida-a {
            left: 0;
            top: 100px;
            background-color: #6ff;
            background: url(../images/1.png) no-repeat -500px -100px / 600px auto;

            /* background-position: -500px -100px; */
            z-index: 1;
        }

        .valida-b {
            background-color: #fff;
            left: 500px;
            top: 100px;
        }

        .valida-slide {
            position: relative;
            height: 45px;
            border-radius: 30px;
            margin-top: 10px;
            background-color: grey;
        }

        .ball {
            border-radius: 50%;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="valida-bar">
        <div class="valida-rand">
            <img src="../images/1.png" alt="">
            <div class="valida-a"></div>
            <div class="valida-b"></div>
        </div>
        <div class="valida-slide">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>
    var validaBar = document.getElementsByClassName("valida-bar")[0];
    var validaRand = document.getElementsByClassName("valida-rand")[0];
    var validaA = document.getElementsByClassName("valida-a")[0];
    var validaB = document.getElementsByClassName("valida-b")[0];
    var ball = document.getElementsByClassName("ball")[0];

    var maxLeft = validaBar.clientWidth - ball.clientWidth;
    var maxTop = validaRand.clientHeight - validaB.clientHeight;

    console.log(maxTop);

    // 随机位置
    // left (200 - maxLeft)
    // top  (0  - maxTop)

    // 问题: 
    // validaRand 由img撑开, 但是在图片没有加载出来之前 => 是无法完全撑开父元素高度的 -> randY有问题

    // 解决方法
    // 1. 提前将图片的高度设置  好

    randPos();

    // var randX = 200 + Math.round(Math.random()*(maxLeft-200));
    // var randY = Math.round(Math.random()*maxTop);

    // validaB.style.left = randX + "px";
    // validaB.style.top = randY + "px";
    // validaA.style.top = randY + "px";

    // // background-position: -500px -100px;
    // validaA.style.backgroundPosition = -randX +"px "+ (-randY) + "px";
    // validaA.style.backgroundPosition = `-${randX}px -${randY}px`;

    // validaA.style.backgroundPositionX = -randX + "px";
    // validaA.style.backgroundPositionY = -randY + "px";

    ball.onmousedown = function (e) {
        var e = e || window.event;
        var posX = e.offsetX;
        var posY = e.offsetY;

        document.onmousemove = function (e) {
            var e = e || window.event;
            var x = e.pageX - validaBar.offsetLeft - posX;
            // if(x<=0) x = 0;
            // if(x>=maxLeft) x = maxLeft;
            x = Math.min(Math.max(0, x), maxLeft);

            ball.style.left = x + "px";
            validaA.style.left = x + "px";
        }

        document.onmouseup = function (e) {
            document.onmousemove = null;


            // 如何验证?
            // 1.  当A B两个盒子 重叠验证成功  => offsetLeft相同的
            // 2.  validaA.offsetLeft == validaB.offsetLeft 理想情况下, 手抖可能不好对齐
            //  设置误差允许范围 
            //  [validaB.offsetLeft-2, validaB.offsetLeft+2]

            if(validaA.offsetLeft){
                if (validaA.offsetLeft >= validaB.offsetLeft - 5 && validaA.offsetLeft <= validaB.offsetLeft + 5) {
                    console.log("验证成功");
                    location.href = "https://www.baidu.com";
                } else {
                    ball.style.left = 0 + "px";
                    validaA.style.left = 0 + "px";
                    randPos();
                }
            }


        }

    }
    function randPos(){
        var randX = 200 + Math.round(Math.random()*(maxLeft-200));
        var randY = Math.round(Math.random()*maxTop);

        validaB.style.left = randX + "px";
        validaB.style.top = randY + "px";
        validaA.style.top = randY + "px";

        // background-position: -500px -100px;
        validaA.style.backgroundPosition = -randX +"px "+ (-randY) + "px";
        validaA.style.backgroundPosition = `-${randX}px -${randY}px`;

        validaA.style.backgroundPositionX = -randX + "px";
        validaA.style.backgroundPositionY = -randY + "px";
    }
</script>

</html>